package client_package;


import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JMenuItem;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.JLabel;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import java.util.GregorianCalendar;


public class VueConversation extends VueAvecBar {
	private static final long serialVersionUID = 1L;
	
	private JPanel contentPane;
	private JScrollPane scroll_pane;
	private JScrollPane scroll_pane2;
	private JTextField textFieldNom;
	
	private JTextArea textAreaSaisie;
	private JTextArea textAreaConverse;
	
	private JLabel lblTourParole;
	private JLabel msgOk;
	
	private JButton btnEnvoyer;
	private JButton btnDconnexion;
	private JButton btnPassezLaMain;
	
	private String loginOppose;
	
	public JTextArea getConverse() {
		return textAreaConverse;
	}

	public VueConversation(Controleur c, String loginOpp) {
		super(c);
		this.loginOppose = loginOpp;
		setResizable(false);
		setBounds(100, 100, 450, 540);
		setTitle("Conversation");
		
		
		
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JMenuItem mntmDconnexion = new JMenuItem("Déconnexion");
		getMnApplication().add(mntmDconnexion, 0);
		mntmDconnexion.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				getControleur().deconnexion();
			}
		});
		
		btnDconnexion = new JButton("Déconnexion");
		btnDconnexion.setBounds(66, 452, 121, 25);
		contentPane.add(btnDconnexion);
		btnDconnexion.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				getControleur().deconnexion();
			}
		});
		
		scroll_pane = new JScrollPane();
		scroll_pane.setBounds(32, 42, 378, 250);
		scroll_pane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		scroll_pane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		contentPane.add(scroll_pane);
		
		textAreaConverse = new JTextArea();
		textAreaConverse.setWrapStyleWord(true);
		textAreaConverse.setEditable(false);
		textAreaConverse.setLineWrap(true);
		textAreaConverse.setBounds(12, 42, 378, 250);
		scroll_pane.setViewportView(textAreaConverse);
		
		
		
		textFieldNom = new JTextField(loginOppose);
		textFieldNom.setEditable(false);
		textFieldNom.setBounds(140, 11, 114, 19);
		contentPane.add(textFieldNom);
		textFieldNom.setColumns(10);
		
		btnEnvoyer = new JButton("Envoyer");
		btnEnvoyer.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String texte = textAreaSaisie.getText();
				getControleur().envoyertexte(texte);
				textAreaSaisie.setText(null);
			}
		});
		btnEnvoyer.setBounds(253, 452, 121, 25);
		contentPane.add(btnEnvoyer);
		
		
		scroll_pane2 = new JScrollPane();
		scroll_pane2.setBounds(55, 358, 331, 77);
		scroll_pane2.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		scroll_pane2.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		contentPane.add(scroll_pane2);
		
		
		textAreaSaisie = new JTextArea();
		textAreaSaisie.setLineWrap(true);
		textAreaSaisie.setWrapStyleWord(true);
		textAreaSaisie.setBounds(35, 304, 331, 77);
		scroll_pane2.setViewportView(textAreaSaisie);
		
		JLabel lblLoginOppos = new JLabel("Login opposé : ");
		lblLoginOppos.setBounds(35, 13, 101, 15);
		contentPane.add(lblLoginOppos);
		
		msgOk = new JLabel("Aucun message envoyé");
		msgOk.setBounds(57, 299, 331, 15);
		contentPane.add(msgOk);
		
		lblTourParole = new JLabel("C'est à votre correspondant de parler");
		lblTourParole.setBounds(12, 326, 331, 15);
		contentPane.add(lblTourParole);
		
		btnPassezLaMain = new JButton("Passer la main");
		btnPassezLaMain.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				getControleur().passerLaMain();
			}
		});
		btnPassezLaMain.setEnabled(false);
		btnPassezLaMain.setBounds(272, 321, 139, 25);
		contentPane.add(btnPassezLaMain);
		textAreaSaisie.addKeyListener( new KeyListener(){
				public void keyPressed(KeyEvent e){
					if (e.isShiftDown() && e.getKeyCode() == KeyEvent.VK_ENTER) {
						textAreaSaisie.append("\n");
					} else if (e.getKeyCode() == KeyEvent.VK_ENTER){
						e.consume();
						String texte = textAreaSaisie.getText();
						getControleur().envoyertexte(texte);
						textAreaSaisie.setText(null);
					} else if (e.getKeyCode() == KeyEvent.VK_ESCAPE){
						getControleur().deconnexion();
					}
				}
				public void keyTyped(KeyEvent e) {}
				public void keyReleased(KeyEvent e) {}
			}
		);
	}
	
	public void alimente(String ch) {
		if(ch.equals("%Serveur% : msg ok")) {
			GregorianCalendar tmp = new GregorianCalendar();
			msgOk.setText("Votre dernier message a été reçu à " +
				tmp.get(GregorianCalendar.HOUR_OF_DAY) + ":" +
				tmp.get(GregorianCalendar.MINUTE) + ":" +
				tmp.get(GregorianCalendar.SECOND));
		} else if(ch.equals("%Serveur% : msg failed")) {
			msgOk.setText("Votre dernier message n'a pas pu être envoyé");
		} else {
			textAreaConverse.append(ch + "\n");
			scroll_pane.getVerticalScrollBar().getModel().setValue(scroll_pane.getVerticalScrollBar().getModel().getMaximum());
		}
	}
	
	public void setPasserLaMain(boolean b)
	{
		btnPassezLaMain.setEnabled(b);
		if(b){
			lblTourParole.setText("C'est à votre tour de parler");
		}
		else{
			lblTourParole.setText("C'est à votre correspondant de parler");
		}

	}
}